package com.lsh.oms.core.service.tool;

import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Tool Service
 * Created by panxudong on 16/8/30.
 */
public class ToolService {

    private static Logger logger = LoggerFactory.getLogger(ToolService.class);

    private static final String SALE_LEVEL = "level";

    private static final String SALE_NAME = "saleInfo";

    private static final String SALE_UID = "uid";

    private static final String LEVEL_1 = "1";

    private static final String LEVEL_2 = "2";

    private static final String LEVEL_3 = "3";

    private static final String LEVEL_4 = "4";

    private static final String LEVEL_5 = "5";

    private static final String LEVEL_6 = "6";

    private static final String LEVEL_7 = "7";

    private static final String LEVEL_DEFAULT_VALUE = "0";

    private static final String REQUEST_HEADER_RANDOM = "random";

    private static final String REQUEST_HEADER_PLATFORM = "platform";

    private static final String STRING_SPLIT = "_";

    public static final ValueFilter valueFilter = new ValueFilter() {
        @Override
        public Object process(Object o, String s, Object o1) {
            Object object = o1;
            if (object == null) {
                return o1;
            }
            if (object.getClass() == Long.class || object.getClass() == Integer.class
                    || object.getClass() == BigDecimal.class) {
                return object.toString();
            }
            return object;
        }
    };

    public static String getLogSign() {
        return getHeader(REQUEST_HEADER_PLATFORM) + STRING_SPLIT + getHeader(REQUEST_HEADER_RANDOM);
    }

    public static String getHeader(String key) {
        HttpServletRequest request = (HttpServletRequest) RpcContext.getContext().getRequest();
        return request.getHeader(key);
    }

    public static String getUrl() {
        HttpServletRequest request = (HttpServletRequest) RpcContext.getContext().getRequest();
        return request.getRequestURI() + "?" + request.getQueryString();
    }

//    public static Header[] getRequestTmsHeader(Long logSign) {
//        BasicHeader logSignHeader = new BasicHeader("logSign", String.valueOf(logSign));
//        return ToolService.getRequestTmsHeader(logSignHeader);
//    }

    public static Header[] getRequestTmsHeader(Long logSign, Long venderId) {
        BasicHeader logSignHeader = new BasicHeader("logSign", String.valueOf(logSign));
        BasicHeader venderHeader = new BasicHeader("venderId", venderId + "");
        return ToolService.getRequestTmsHeader(logSignHeader, venderHeader);
    }

//    public static Header[] getRequestTmsHeader(String logSign) {
//        BasicHeader logSignHeader = new BasicHeader("logSign", logSign);
//        return ToolService.getRequestTmsHeader(logSignHeader);
//    }

    public static Header[] getRequestTmsHeader(String logSign, Long venderId) {
        BasicHeader logSignHeader = new BasicHeader("logSign", logSign);
        BasicHeader venderHeader = new BasicHeader("venderId", venderId + "");
        return ToolService.getRequestTmsHeader(logSignHeader, venderHeader);
    }

    private static Header[] getRequestTmsHeader(BasicHeader logSignHeader) {
        Header[] headers = {logSignHeader, new BasicHeader("Accept", "application/json")};
        return headers;
    }

    private static Header[] getRequestTmsHeader(BasicHeader logSignHeader, BasicHeader venderHeader) {
        Header[] headers = {logSignHeader, venderHeader, new BasicHeader("Accept", "application/json")};
        return headers;
    }

    public static Set<String> contentToSet(String content) {
        String[] contentArray = content.split(",");

        return new HashSet<>(Arrays.asList(contentArray));
    }

    public static String getEsHits(String content) {
        JSONObject contentObject = JSONObject.parseObject(content);
        JSONObject hitsObject = JSONObject.parseObject(contentObject.getString("hits"));
        return hitsObject.getString("hits");
    }

    public static String getEsTotal(String content) {
        JSONObject contentObject = JSONObject.parseObject(content);
        JSONObject hitsObject = JSONObject.parseObject(contentObject.getString("hits"));
        return hitsObject.getString("total");
    }

    public static String getTimeByStamp(Integer timestamp) {
        return getTimeByStamp(timestamp * 1000L);
    }

    public static String getTimeByStamp(Long timestamp) {
        Date date = new Date(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String transactSQLInjection(String str) {
        return str.replaceAll(".*([';]+|(--)+).*", "");
    }

}
